﻿Public Class frmNewDepatment

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Me.txtDeptDescription.Text = "" Then
            MsgBox("Please Enter Data to Department!", MsgBoxStyle.Critical, "Enter Department")
            txtDeptDescription.Focus()
        ElseIf txtDeptNote.Text = "" Then
            MsgBox("Please Enter Data to Note!", MsgBoxStyle.Critical, "Enter Department")
            txtDeptNote.Focus()
        Else
            Dim SQLTest As String
            SQLTest = "INSERT INTO tbl_department(DeptID,DeptName,DeptNote) VALUES (" & txtDepID.Text & ",'" & txtDeptDescription.Text & "','" & txtDeptNote.Text & "')"
            SaveDataToAccess(SQLTest)
            Cleartext()

            Me.txtDepID.Text = AutoID4Access("SELECT DeptID FROM tbl_department", "tbl_department") + 1
            txtDepID.Enabled = False
            txtDeptDescription.Focus()
        End If

    End Sub
    Sub Cleartext()
        Me.txtDepID.Text = ""
        Me.txtDeptDescription.Text = ""
        Me.txtDeptNote.Text = ""
    End Sub
    Private Sub frmNewDepatment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Connect()
        LoadDataToLVDept("SELECT * FROM tbl_department", LVDepartement)
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Cleartext()
        Me.txtDepID.Text = AutoID4Access("SELECT DeptID FROM tbl_department", "tbl_department") + 1
        txtDepID.Enabled = False
        txtDeptDescription.Focus()
        btnDelete.Enabled = False
        btnEdit.Enabled = False
    End Sub
    Private Sub LVDepartement_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LVDepartement.MouseDoubleClick

        Dim i As Integer
        For i = 0 To Me.LVDepartement.Items.Count - 1
            If LVDepartement.Items(i).Selected = True Then
                Me.txtDepID.Text = LVDepartement.Items(i).Text
                Me.txtDeptDescription.Text = LVDepartement.Items(i).SubItems(1).Text
                Me.txtDeptNote.Text = LVDepartement.Items(i).SubItems(2).Text
            End If
        Next
        btnDelete.Enabled = True
        btnEdit.Enabled = True
        btnSave.Enabled = False

    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        DeleteDataAccess("DELETE * FROM tbl_department WHERE DeptID=" & txtDepID.Text)
        LoadDataToLVDept("SELECT * FROM tbl_department", LVDepartement)
        Cleartext()
        btnDelete.Enabled = False
        btnSave.Enabled = False
        btnEdit.Enabled = False

    End Sub

    Private Sub LVDepartement_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LVDepartement.SelectedIndexChanged

    End Sub
End Class